package com.wan37.logic.handler.server;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.fighting.auto.AutoSkillServiceManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.WorkerHandler;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.AllRoleLabel;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author : luoyong
 * @date : 2020-07-10 11:36
 **/
public class DestroySceneHandler implements WorkerHandler {
    private SceneServiceCache sceneServiceCache;
    private UserServiceCache userServiceCache;
    private String sceneId;

    public DestroySceneHandler(String sceneId) {
        this.sceneId = sceneId;
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        /**
         * 销毁临时场景
         */
        if (sceneServiceCache.getSceneServiceMap().containsKey(sceneId)) {
            if (sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet().size() != 0){
                return;
            }
            String preSceneId = sceneServiceCache.getSceneServiceMap().get(sceneId).getPreSceneId();
            sceneServiceCache.sceneSet.remove(sceneId);
            sceneServiceCache.sceneNameToIdMap.remove(sceneId);
            sceneServiceCache.getSceneServiceMap().remove(sceneId);
            sceneServiceCache.getSceneServiceMap().get(preSceneId).getGoSceneIdSet().remove(sceneId);
//            /**
//             * 销毁实体对应的技能优先级队列，防止内存泄漏
//             */
//            for (String userId : sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet()){
//                for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()){
//                    AutoSkillServiceManager.allRoleIdToPBQMap.remove(roleId);
//                }
//            }
//            for (AllRoleVO allRoleVO : sceneServiceCache.getSceneServiceMap().get(sceneId).getAllRoleVOMap().values()){
//                if (allRoleVO.getLabel() == AllRoleLabel.MONSTER){
//                    AutoSkillServiceManager.allRoleIdToPBQMap.remove(allRoleVO.getAllRoleId());
//                }
//            }
        }
    }
}
